﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.BLL;
using BotonJXC.Entity;
using NHibernate;
using BotonJXC.DAO;
using ComponentFactory.Krypton.Toolkit;

namespace BotonJXC.Forms.Controls
{
    public partial class ProcessFormulaControl : BaseInfoControl
    {
        ProcessFormulaBLL _processFormulaBll = new ProcessFormulaBLL();
        string _itemName;
        short _itemType;
        IList<ProcessFormula> _itemDatas;
        public ProcessFormulaControl()
        {
            InitializeComponent();
        }

        private void ProcessFormulaControl_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            PageModel<ProcessFormula> pageModel = _processFormulaBll.Find(_itemName, _itemType, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            _itemDatas = pageModel.Results;
            kdgvProcessFormula.DataSource = new BindingList<ProcessFormula>(_itemDatas);
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _itemName = ktbFormulaName.Text.Trim();
            _itemType = (short)(kcbFormulaType.SelectedIndex + 1);
        }

        private void kdgvProcessFormula_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && _itemDatas.Count > e.RowIndex)
            {
                e.Value = _itemDatas[e.RowIndex].FormulaType == 1 ? "加工" : "出品";
            }
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvProcessFormula.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void AddInfo()
        {
            base.AddInfo();
        }

        public override void DeleteInfo()
        {
            IList<ProcessFormula> units = (IList<ProcessFormula>)kdgvProcessFormula.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvProcessFormula.SelectedRows)
                {
                    ProcessFormula item = r.DataBoundItem as ProcessFormula;
                    if (item != null)
                    {
                        _processFormulaBll.Delete(item);
                    }
                    units.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvProcessFormula.Refresh();
        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvProcessFormula.Rows)
                {
                    ProcessFormula u = r.DataBoundItem as ProcessFormula;
                    if (u == null)
                        continue;
                    if (u.FormulaId == 0)
                    {
                        _processFormulaBll.Save(u);
                    }
                    else if ("1".Equals(r.Tag) && u.FormulaId != 0)
                    {
                        _processFormulaBll.Update(u);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }

        private void kdgvProcessFormula_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvProcessFormula.Rows[e.RowIndex].Tag = "1";
        }

        private void kdgvProcessFormula_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (kdgvProcessFormula.CurrentCell.ColumnIndex == 1)
            {
                KryptonDataGridViewComboBoxEditingControl cbc = e.Control as KryptonDataGridViewComboBoxEditingControl;
                cbc.DropDownClosed += new EventHandler(cbc_DropDownClosed);
            }
        }

        void cbc_DropDownClosed(object sender, EventArgs e)
        {
            KryptonDataGridViewComboBoxEditingControl cbc = sender as KryptonDataGridViewComboBoxEditingControl;
            IList<ProcessFormula> itemDatas = (IList<ProcessFormula>)kdgvProcessFormula.DataSource;
            itemDatas[kdgvProcessFormula.CurrentCell.RowIndex].FormulaType = (short)(cbc.SelectedIndex + 1);
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
